"""

"""
import numpy as np

from common.functions import softmax_function_v2
from common.loss_functions import cross_entropy_error


class Relu:
    def __init__(self):
        """
        mask:
            用来保存正向传播的时候输入 x 元素中，< 0 的地方保存为 true, >= 保存为 False
        """
        self.mask = None

    def forward(self, x):
        self.mask = (x <=0)
        out = x.copy()
        out[self.mask] = 0

        return out

    def backward(self,dout):
        dout[self.mask] = 0
        dx = dout
        return dx

class Sigmoid:
    def __init__(self):
        self.out =None

    def forward(self,x):
        out = 1/(1 + np.exp(-x))
        self.out = out
        return out

    def backward(self,dout):
        dx= dout * (1.0 -self.out) * self.out
        return dx

class Affine:
    def __init__(self,W,b):
        self.W = W
        self.b = b
        self.x = None
        self.dW = None
        self.db = None

    def forward(self,x):
        self.x = x
        out = np.dot(x, self.W) + self.b
        return out

    def backward(self,dout):
        dx = np.dot(dout, self.W.T)
        self.dW = np.dot(self.x.T, dout)
        self.db = np.sum(dout, axis=0)
        return dx

class SoftmaxWithLoss:
    def __init__(self):
        self.loss = None    # 损失
        self.y = None       # softmax 的输出
        self.t = None       # 监督数据(one=hot vector)

    def forward(self,x,t):
        self.t = t
        self.y = softmax_function_v2(x)
        self.loss =cross_entropy_error(self.y,self.t)
        return self.loss

    def backward(self,dout=1):
        batch_size = self.t.shape[0]
        dx = (self.y - self.t)/batch_size

        return dx

if __name__=='__main__':
    x = np.array([[1,-0.5],[2,3]])
    mask =(x <= 0)
    print(mask)